﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-5-12</createdate>
//<author>zhengw</author>
//<email>zhengw@tunynet.com</email>
//<log date="2008-5-12">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Caching;
using Tunynet.QingdaoCustoms.Common;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using System.Collections.ObjectModel;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 活动原型业务逻辑类
    /// </summary>
    public class ActivityPrototypes
    {
        /// <summary>
        /// 创建活动原型
        /// </summary>
        public static void CreateActivityPrototype(ActivityPrototypeInfo activityPrototypeInfo)
        {
            WorkflowTrackingDataProvider.Instance().CreateUpdateActivityPrototype(activityPrototypeInfo, DataProviderAction.Create);

            #region 清除缓存
            WebCache.Remove(string.Format("ActivityPrototypes:{0}", activityPrototypeInfo.WorkflowGuid.ToString()));
            #endregion
        }

        /// <summary>
        /// 更新活动原型
        /// </summary>
        public static void UpdateActivityPrototype(ActivityPrototypeInfo activityPrototypeInfo)
        {
            WorkflowTrackingDataProvider.Instance().CreateUpdateActivityPrototype(activityPrototypeInfo, DataProviderAction.Update);

            #region 清除缓存
            WebCache.Remove(string.Format("ActivityPrototypes:{0}", activityPrototypeInfo.WorkflowGuid.ToString()));
            WebCache.Remove(string.Format("ActivityPrototype:{0}", activityPrototypeInfo.PrototypeGuid.ToString()));
            #endregion
        }

        /// <summary>
        /// 获取活动原型
        /// </summary>
        /// <param name="prototypeGuid">活动原型ID</param>
        public static ActivityPrototypeInfo GetActivityPrototype(Guid prototypeGuid)
        {
            string cacheKey = string.Format("ActivityPrototype:{0}", prototypeGuid.ToString());

            ActivityPrototypeInfo activityPrototype = null;
            activityPrototype = WebCache.Get(cacheKey) as ActivityPrototypeInfo;

            if (activityPrototype == null)
            {
                activityPrototype = WorkflowTrackingDataProvider.Instance().GetActivityPrototype(prototypeGuid);
                WebCache.Insert(cacheKey, activityPrototype, WebCache.MinuteFactor * 10);
            }

            return activityPrototype;
        }


        /// <summary>
        /// 获取工作流实例的所有活动原型
        /// </summary>
        private static List<ActivityPrototypeInfo> GetActivityPrototypes(Guid workflowGuid)
        {
            string cacheKey = string.Format("ActivityPrototypes:{0}", workflowGuid.ToString());

            List<ActivityPrototypeInfo> activityPrototypes = null;
            activityPrototypes = WebCache.Get(cacheKey) as List<ActivityPrototypeInfo>;

            if (activityPrototypes == null)
            {
                activityPrototypes = WorkflowTrackingDataProvider.Instance().GetActivityPrototypes(workflowGuid);
                WebCache.Insert(cacheKey, activityPrototypes, WebCache.MinuteFactor * 30);
            }

            return activityPrototypes;
        }

        /// <summary>
        /// 获取主流程中工作流实例的所有活动原型
        /// </summary>
        public static List<ActivityPrototypeInfo> GetActivityPrototypesOfMainFlow(Guid workflowGuid)
        {
            List<ActivityPrototypeInfo> allActivityPrototypes = GetActivityPrototypes(workflowGuid);
            List<ActivityPrototypeInfo> activityPrototypes = new List<ActivityPrototypeInfo>();

            foreach (ActivityPrototypeInfo activityPrototype in allActivityPrototypes)
            {
                if (activityPrototype.SubFlowGuid == Guid.Empty)
                    activityPrototypes.Add(activityPrototype);
            }

            return activityPrototypes;
        }

        /// <summary>
        /// 获取子流程中工作流实例的所有活动原型
        /// </summary>
        public static List<ActivityPrototypeInfo> GetActivityPrototypesOfSubFlow(Guid workflowGuid, Guid subFlowGuid)
        {
            List<ActivityPrototypeInfo> allActivityPrototypes = GetActivityPrototypes(workflowGuid);
            List<ActivityPrototypeInfo> activityPrototypes = new List<ActivityPrototypeInfo>();

            foreach (ActivityPrototypeInfo activityPrototype in allActivityPrototypes)
            {
                if (activityPrototype.SubFlowGuid == subFlowGuid)
                    activityPrototypes.Add(activityPrototype);
            }

            return activityPrototypes;
        }


        /// <summary>
        /// 获取主流程中的第一个活动原型
        /// </summary>
        public static ActivityPrototypeInfo GetFirstActivityPrototypeOfMainFlow(Guid workflowGuid)
        {
            List<ActivityPrototypeInfo> activityPrototypes = GetActivityPrototypesOfMainFlow(workflowGuid);

            ActivityPrototypeInfo first = null;

            if (activityPrototypes != null && activityPrototypes.Count > 0)
                first = activityPrototypes[0];

            return first;
        }

        /// <summary>
        /// 获取子流程中的第一个活动原型
        /// </summary>
        public static ActivityPrototypeInfo GetFirstActivityPrototypeOfSubFlow(Guid workflowGuid, Guid subFlowGuid)
        {
            List<ActivityPrototypeInfo> activityPrototypes = GetActivityPrototypesOfSubFlow(workflowGuid, subFlowGuid);

            ActivityPrototypeInfo first = null;

            if (activityPrototypes != null && activityPrototypes.Count > 0)
                first = activityPrototypes[0];

            return first;
        }


        /// <summary>
        /// 获取下一个活动原型
        /// </summary>
        public static ActivityPrototypeInfo GetNextActivityPrototype(Guid workflowGuid, Guid subFlowGuid, string codeName)
        {
            List<ActivityPrototypeInfo> activityPrototypes = null;

            if (subFlowGuid != Guid.Empty)
                activityPrototypes = GetActivityPrototypesOfSubFlow(workflowGuid, subFlowGuid);
            else
                activityPrototypes = GetActivityPrototypesOfMainFlow(workflowGuid);

            ActivityPrototypeInfo nextActivityPrototype = null;
            int currentIndex = -1;

            for (int i = 0; i < activityPrototypes.Count; i++)
            {
                if (activityPrototypes[i].CodeName.Equals(codeName, StringComparison.CurrentCultureIgnoreCase))
                {
                    currentIndex = i;
                    break;
                }
            }

            if (currentIndex > -1)
            {
                if (currentIndex + 1 < activityPrototypes.Count)
                    nextActivityPrototype = activityPrototypes[currentIndex + 1];
            }

            return nextActivityPrototype;
        }

        /// <summary>
        /// 获取前一个活动原型
        /// </summary>
        public static ActivityPrototypeInfo GetPreviousActivityPrototype(Guid workflowGuid, string codeName, Guid subFlowGuid)
        {
            List<ActivityPrototypeInfo> activityPrototypes = null;

            if (subFlowGuid != Guid.Empty)
                activityPrototypes = GetActivityPrototypesOfSubFlow(workflowGuid, subFlowGuid);
            else
                activityPrototypes = GetActivityPrototypesOfMainFlow(workflowGuid);

            ActivityPrototypeInfo previousActivityPrototype = null;

            int currentIndex = -1;

            for (int i = 0; i < activityPrototypes.Count; i++)
            {
                if (activityPrototypes[i].CodeName.Equals(codeName, StringComparison.CurrentCultureIgnoreCase))
                {
                    currentIndex = i;
                }
            }

            if (currentIndex > -1)
            {
                if (currentIndex - 1 >= 0)
                    previousActivityPrototype = activityPrototypes[currentIndex - 1];
            }

            return previousActivityPrototype;
        }

        /// <summary>
        /// 判断角色内是否有用户存在
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public static bool HasUsersInRoles(string roles,string organizationIDs,string applicationCodeName)
        {
            ReadOnlyCollection<IUser> executors = null;

            string[] roleArray = null;
            if (string.IsNullOrEmpty(roles.Trim())==false)
            {
                roleArray = roles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }

            string[] organizationIDArray = null;
            if (string.IsNullOrEmpty(organizationIDs) == false)
            {
                organizationIDArray = organizationIDs.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
            }
            
            executors = new ReadOnlyCollection<IUser>(ChinaCustomsPermissionWrapper.GetUsers(applicationCodeName, roleArray, organizationIDArray));

            if (executors == null || executors.Count == 0)
            {
                return false;
            }

            return true;
        }

    }
}
